// import RNAlibcSdk from 'react-native-alibaichuan';
import TbParams from './TbParams';
import {TimeStamp} from '../utils/utils';
import md5 from 'md5';
import {mkParamTo} from './TbParams';
import AlibcaMgr from '../sdkManager/alibabaManager';
/**
 * 跳转二合一领券地址
 * @constructor
 */
export function JumpTo(item) {
  //初始化
  AlibcaMgr.asyncInit((code, map) => {
    // console.log(typeof code);
    if (code !== 0) {
      return;
    }
    AlibcaMgr.isLogin((code, param) => {
      if (code === 0) {
        //已经登录
        toTaoBao(item);
      } else {
        //登录
        AlibcaMgr.showLogin((code, param) => {
          //显示
          toTaoBao(item);
        });
      }
    });
  });
}

export function toShop(id) {
    //初始化
    AlibcaMgr.asyncInit((code, map) => {
        // console.log(typeof code);
        if (code !== 0) {
            return;
        }
        AlibcaMgr.isLogin((code, param) => {
            if (code === 0) {
                //已经登录
                _toShop(id);
            } else {
                //登录
                AlibcaMgr.id((code, param) => {
                    //显示
                    _toShop(item);
                });
            }
        });
    });
}

 function _toShop(id) {
    //   console.log(item);
    var parm = {
        shopid:id,
        BizCodeStr:"shop"
    };
    var params = mkParamTo(parm);
    //   console.log(params);
    AlibcaMgr.showPage(
        {
            type: 'shop', //detail shop mycard url
            payload: params,
        },
        (code, info) => {
            console.log(code)
            console.log(info)
        },
    );
}

function toTaoBao(item) {
    console.log(item);
  var parm = {
    url: item.coupon_click_url,
  };
  var params = mkParamTo(parm);
  //   console.log(params);
  AlibcaMgr.showPage(
    {
      type: 'url', //detail shop mycard url
      payload: params,
    },
    (code, info) => {},
  );
}

export function Sign(appKey, secretKey, method, args) {
  args.app_key = appKey;
  args.format = 'json';
  args.method = method;
  args.sign_method = 'md5';
  args.timestamp = TimeStamp();
  args.v = '2.0';

  var sorted = Object.keys(args).sort();
  var basestring = secretKey;
  for (var i = 0, l = sorted.length; i < l; i++) {
    var k = sorted[i];
    basestring += k + args[k];
  }
  basestring += secretKey;
  args.sign = md5(basestring).toUpperCase();
  return args;
}
